﻿using daonaite.Model.system.dictType;
using daonaite.services.system;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace daonaite.api.Controllers.system
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DictTypeController : BaseController
    {
        private readonly dicttypeService ttcz;

        public DictTypeController(dicttypeService ttcz)
        {
            this.ttcz = ttcz;
        }

        /// <summary>
        /// 查询字典类型
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> select([FromBody] Dto_dicttype data)
        {
            RefAsync<int> total = new RefAsync<int>();
            var list = await ttcz.select(data, total);
            return Success(list, data.pageSize, data.pageIndex, total.Value);
        }

        /// <summary>
        /// 根据id查询字典类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> get(int id)
        {
            var count = await ttcz.get(id);
            return Success(count);
        }

        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> add([FromBody] Dto_dicttype data)
        {
            sys_dicttype dicttype = new sys_dicttype();
            copy<Dto_dicttype, sys_dicttype>(data, dicttype, true);
            RefAsync<string> errmessage = new RefAsync<string>();
            var b = await ttcz.add(dicttype, errmessage);
            if (b)
            {
                return Success();
            }
            else
            {
                return Error(errmessage);
            }
        }

        /// <summary>
        /// 更新字典类型
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> update([FromBody] Dto_dicttype data)
        {
            sys_dicttype dicttype = new sys_dicttype();
            copy<Dto_dicttype, sys_dicttype>(data, dicttype, false, true);
            RefAsync<string> errmessage = new RefAsync<string>();
            var b = await ttcz.updateAsync(dicttype, errmessage);
            if (b)
            {
                return Success();
            }
            else
            {
                return Error(errmessage);
            }
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> delete(int id)
        {
            var count = await ttcz.deleteAsync(id);
            return Success(count);
        }

    }
}
